/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const assert = require('assert');
const addon = require('../build/Release/test_msg_napi');

describe('NAPI Tests Duration', function () {
    it('should create a Duration message with correct type', function () {
        const duration = new addon.MessageDuration();
        assert.strictEqual(duration.__type, 'Duration');
    });

    it('should get and set secs', function () {
        const duration = new addon.MessageDuration();
        duration.secs = 1;
        assert.strictEqual(duration.secs, 1);
    });

    it('should get and set nano_secs', function () {
        const duration = new addon.MessageDuration();
        duration.nano_secs = 2;
        assert.strictEqual(duration.nano_secs, 2);
    });

    it('should encode', function () {
        const duration = new addon.MessageDuration();
        duration.secs = 1;
        duration.nano_secs = 2;
        const encoded = duration.Encode();
        assert.strictEqual(encoded.length, 8);
        assert.strictEqual(encoded[0], 0x01);
        assert.strictEqual(encoded[1], 0x00);
        assert.strictEqual(encoded[2], 0x00);
        assert.strictEqual(encoded[3], 0x00);
        assert.strictEqual(encoded[4], 0x02);
        assert.strictEqual(encoded[5], 0x00);
        assert.strictEqual(encoded[6], 0x00);
        assert.strictEqual(encoded[7], 0x00);
    });

    it('should decode', function () {
        const encoded = new Uint8Array([0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]);
        const duration = new addon.MessageDuration();
        duration.Decode(encoded);
        assert.strictEqual(duration.secs, 1);    
        assert.strictEqual(duration.nano_secs, 2);
        assert.strictEqual(duration.__type, 'Duration');
    });
});
